// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { Database } from './database';
import { Service } from './service';
import { Project } from './interface';
import { Webview } from './webview';
import { Server } from './server';



import { TreeViewProvider } from './TreeDataProvider';
import { Event } from './event';



// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

	// Use the console to output diagnostic information (console.log) and errors (console.error)
	// This line of code will only be executed once when your extension is activated
	console.log('Congratulations, your extension "fast-mock" is now active!');

	const server = new Server();
	server.startServer();

	const database = Database.getSingle();
	database.createDatabase(context);

	const treeViewProvider = new TreeViewProvider();

	vscode.window.registerTreeDataProvider('fast-mock', treeViewProvider);

	const event = Event.getSingle();
	event.on("project-refresh", () => {
		treeViewProvider.refresh();
	});

	vscode.commands.registerCommand('mock.addProject', () => {
		const webview = Webview.getSingle();
		const iconPath = path.join(context.extensionPath, './src/assets/project.svg');
		webview.open("http://localhost:8000/project", "fast-mock-project", "新建项目", iconPath, context);
	});

	vscode.commands.registerCommand('mock.copyUrl', async (treeItemNode) => {
		const service = new Service();
		const { projectCode, url } = await service.queryInterface(treeItemNode.id);
		const path = `http://localhost:3008/api/mock/${projectCode}/${url}`;
		await vscode.env.clipboard.writeText(path);
		vscode.window.showInformationMessage("复制成功!" + path);
		console.log(treeItemNode);
	});


	vscode.commands.registerCommand('mock.test', async (treeItemNode) => {
		const webview = Webview.getSingle();
		const service = new Service();
		const { projectCode, url } = await service.queryInterface(treeItemNode.id);
		const iconPath = path.join(context.extensionPath, './src/assets/test.svg');
		const pathname = `/api/mock/${projectCode}/${url}`;
		webview.open(`http://localhost:8000/test?url=${pathname}`, "fast-mock-project", "添加接口", iconPath, context);
	});

	vscode.commands.registerCommand('mock.addAPI', (treeItemNode) => {
		const webview = Webview.getSingle();
		const iconPath = path.join(context.extensionPath, './src/assets/api.svg');
		webview.open(`http://localhost:8000/interface?projectId=${treeItemNode.id}`, "fast-mock-project", "添加接口", iconPath, context);
	});


	// The command has been defined in the package.json file
	// Now provide the implementation of the command with registerCommand
	// The commandId parameter must match the command field in package.json
	let disposable = vscode.commands.registerCommand('fast-mock.helloWorld', () => {
		// The code you place here will be executed every time your command is executed

		// Display a message box to the user
		vscode.window.showInformationMessage('Hello World from fast-mock!');
	});

	context.subscriptions.push(disposable);
}

// this method is called when your extension is deactivated
export function deactivate() { }
